import request, { downloadFile } from '@/utils/request'
import type {
  LoginRequest,
  LoginResponse,
  RegisterRequest,
  UserInfo,
  UserMenuResponse,
  ApiResponse
} from '@/types/api'
import type { OnlineUser, OnlineUserDetail } from '@/types/system'

// 认证相关API
export const authApi = {
  // 用户登录
  login: (data: LoginRequest): Promise<ApiResponse<LoginResponse>> => {
    return request.post('/auth/login', data)
  },

  // 用户注册
  register: (data: RegisterRequest): Promise<ApiResponse<void>> => {
    return request.post('/auth/register', data)
  },

  // 发送邮箱验证码
  sendEmailCode: (email: string): Promise<ApiResponse<void>> => {
    return request.post('/auth/send-email-code', null, { params: { email } })
  },

  // 刷新令牌
  refreshToken: (data: { refreshToken: string; tenantId?: string }): Promise<ApiResponse<LoginResponse>> => {
    return request.post('/auth/refresh', data)
  },

  // 用户登出
  logout: (data?: { logoutReason?: string; accessToken?: string; sessionId?: string }): Promise<ApiResponse<string>> => {
    return request.post('/auth/logout', data)
  },

  // 获取当前用户信息
  getCurrentUser: (): Promise<ApiResponse<UserInfo>> => {
    return request.get('/auth/me')
  },

  // 强制用户登出 (管理员)
  forceUserLogout: (userId: string, reason?: string): Promise<ApiResponse<string>> => {
    return request.post('/auth/force-logout', null, {
      params: { userId, reason }
    })
  },

  // 获取在线用户统计
  getOnlineUserCount: (): Promise<ApiResponse<number>> => {
    return request.get('/auth/online-count')
  },

  // 获取在线用户详细信息
  getOnlineUserDetails: (includeDetails: boolean = true): Promise<ApiResponse<OnlineUserDetail>> => {
    return request.get('/auth/online-details', {
      params: { includeDetails }
    })
  },

  // 管理员：获取租户活跃会话列表
  getTenantSessions: (params?: { limit?: number }): Promise<ApiResponse<OnlineUser[]>> => {
    return request.get('/auth/sessions/tenant', { params })
  },

  // 超级管理员：获取全局活跃会话列表
  getAllSessions: (params?: { limit?: number }): Promise<ApiResponse<OnlineUser[]>> => {
    return request.get('/auth/sessions/all', { params })
  },

  // 获取当前用户权限集
  getCurrentUserPermissions: (): Promise<ApiResponse<string[]>> => {
    return request.get('/auth/permissions/current')
  },

  // 初始化：重置管理员密码为 admin123（开放接口）
  initResetAdminPassword: (): Promise<ApiResponse<string>> => {
    return request.put('/auth/init/reset-admin-password')
  },

  // 封禁用户（管理员） - 使用Sa-Token原生API
  banUser: (userId: string, reason?: string, banDays: number = -1): Promise<ApiResponse<string>> => {
    return request.post('/auth/user/ban', null, {
      params: { userId, reason, banDays }
    })
  },

  // 解封用户（管理员） - 使用Sa-Token原生API
  unbanUser: (userId: string): Promise<ApiResponse<string>> => {
    return request.post('/auth/user/unban', null, {
      params: { userId }
    })
  },

  // 刷新用户权限（管理员）
  refreshUserPermissions: (userId: string): Promise<ApiResponse<string>> => {
    return request.post('/auth/user/refresh-permissions', null, {
      params: { userId }
    })
  }
}

export default authApi
